import 'dart:io';

import 'package:device_info/device_info.dart';
import 'package:flutter/material.dart';
import 'package:location/location.dart';

class DevicePage extends StatefulWidget {
  @override
  _DevicePageState createState() => new _DevicePageState();
}

class _DevicePageState extends State<DevicePage> {

  String currentLocationLatitude;
  String currentLocationLongitude;
  String currentLocationAccuracy;
  String currentLocationAltitude;
  String currentLocationSpeed;
  String currentLocationSpeedAccuracy;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("Device"),
        ),
        body: Container(
          child: Column(
            children: <Widget>[
              RaisedButton(
                child: Text("点这"),
                onPressed: () => getDeviceInfo(),
              ),
              _showInfo(),
              RaisedButton(
                child: Text("点这"),
                onPressed: () => getLocation(),
              ),
              Text(currentLocationLatitude),
              Text(currentLocationLongitude),
              Text(currentLocationAccuracy),
              Text(currentLocationAltitude),
              Text(currentLocationSpeed),
              Text(currentLocationSpeedAccuracy), //
            ],
          ),
        ),
      ),
    );
  }

  IosDeviceInfo _iosInfo;
  AndroidDeviceInfo _androidInfo;

  void getDeviceInfo() async {
    DeviceInfoPlugin deviceInfo = new DeviceInfoPlugin();
    if (Platform.isIOS) {
      print('IOS设备：');
      IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
    } else if (Platform.isAndroid) {
      print('Android设备');
      AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
      setState(() {
        _androidInfo = androidInfo;
      });
    }
  }

  Widget _showInfo(){
    return _androidInfo != null ? Container(
      child: Column(
        children: <Widget>[
          Text('$_androidInfo'),
          Text('android ' + _androidInfo.version.release),
          Text(_androidInfo.board),
          Text(_androidInfo.bootloader),
          Text(_androidInfo.brand),
          Text(_androidInfo.device),
          Text(_androidInfo.display),
          Text(_androidInfo.fingerprint),
          Text(_androidInfo.hardware),
          Text(_androidInfo.host),
          Text(_androidInfo.id),
          Text(_androidInfo.manufacturer),
          Text(_androidInfo.model),
          Text(_androidInfo.product),
          Text(_androidInfo.supported32BitAbis.toString()),
          Text(_androidInfo.supported64BitAbis.toString()),
          Text(_androidInfo.supportedAbis.toString()),
          Text(_androidInfo.tags.toString()),
          Text(_androidInfo.type),
          Text(_androidInfo.isPhysicalDevice.toString()),
        ],
      ),
    ) : Container();
  }

  var currentLocation = <String, double>{};
  Location location = new Location();

  bool _serviceEnabled;
  PermissionStatus _permissionGranted;
  LocationData _locationData;

  void getLocation() async{
    _serviceEnabled = await location.serviceEnabled();
    if (!_serviceEnabled) {
      _serviceEnabled = await location.requestService();
      if (!_serviceEnabled) {
        return;
      }
    }

    _permissionGranted = await location.hasPermission();
    if (_permissionGranted == PermissionStatus.denied) {
      _permissionGranted = await location.requestPermission();
      if (_permissionGranted != PermissionStatus.granted) {
        return;
      }
    }

    _locationData = await location.getLocation();

    
    location.onLocationChanged.listen((LocationData currentLocation) {
      setState(() {
        currentLocationLatitude = currentLocation.latitude.toString();
        currentLocationLongitude = currentLocation.longitude.toString();
        currentLocationAccuracy = currentLocation.accuracy.toString();
        currentLocationAltitude = currentLocation.altitude.toString();
        currentLocationSpeed = currentLocation.speed.toString();
        currentLocationSpeedAccuracy = currentLocation.speedAccuracy.toString(); //
      });
    });

  }


}

